home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / MACVOGL- / EXAMPLES / CUBE.C < prev    next >
C/C++ Source or Header  |  1992-07-19  |  9KB  |  487 lines

  1.  
  2. #include <stdio.h>
  3.  
  4. #ifdef SGI
  5. #include "gl.h"
  6. #include "device.h"
  7. #else
  8. #include "vogl.h"
  9. #include "vodevice.h"
  10. #endif
  11.  
  12. Coord carray[][3] = { -1.0,  -1.0,   1.0, /* front */
  13.               1.0,  -1.0,   1.0,
  14.               1.0,   1.0,   1.0,
  15.              -1.0,   1.0,   1.0,
  16.              -1.0,  -1.0,  -1.0, /* rear */
  17.               1.0,  -1.0,  -1.0,
  18.               1.0,   1.0,  -1.0,
  19.              -1.0,   1.0,  -1.0
  20.         };
  21.  
  22. int    nplanes;
  23.  
  24. /*
  25.  * drawcube
  26.  *
  27.  *    draw the cube setting colours if available
  28.  */
  29. void
  30. drawcube()
  31. {
  32.     if (nplanes > 1)
  33.         color(RED);
  34.  
  35.     /* Front */
  36.     pmv(carray[0][0], carray[0][1], carray[0][2]);
  37.     pdr(carray[1][0], carray[1][1], carray[1][2]);
  38.     pdr(carray[2][0], carray[2][1], carray[2][2]);
  39.     pdr(carray[3][0], carray[3][1], carray[3][2]);
  40.     pclos();
  41.     
  42.     if (nplanes > 1)
  43.         color(GREEN);
  44.  
  45.     /* Back */
  46.     pmv(carray[5][0], carray[5][1], carray[5][2]);
  47.     pdr(carray[4][0], carray[4][1], carray[4][2]);
  48.     pdr(carray[7][0], carray[7][1], carray[7][2]);
  49.     pdr(carray[6][0], carray[6][1], carray[6][2]);
  50.     pclos();
  51.  
  52.     if (nplanes > 1)
  53.         color(YELLOW);
  54.  
  55.     /* Right side */
  56.     pmv(carray[1][0], carray[1][1], carray[1][2]);
  57.     pdr(carray[5][0], carray[5][1], carray[5][2]);
  58.     pdr(carray[6][0], carray[6][1], carray[6][2]);
  59.     pdr(carray[2][0], carray[2][1], carray[2][2]);
  60.     pclos();
  61.  
  62.     if (nplanes > 1)
  63.         color(BLUE);
  64.  
  65.     /* Left side */
  66.     pmv(carray[0][0], carray[0][1], carray[0][2]);
  67.     pdr(carray[3][0], carray[3][1], carray[3][2]);
  68.     pdr(carray[7][0], carray[7][1], carray[7][2]);
  69.     pdr(carray[4][0], carray[4][1], carray[4][2]);
  70.     pclos();
  71.  
  72.     if (nplanes > 1)
  73.         color(MAGENTA);
  74.  
  75.     /* Top */
  76.     pmv(carray[2][0], carray[2][1], carray[2][2]);
  77.     pdr(carray[6][0], carray[6][1], carray[6][2]);
  78.     pdr(carray[7][0], carray[7][1], carray[7][2]);
  79.     pdr(carray[3][0], carray[3][1], carray[3][2]);
  80.     pclos();
  81.     
  82.     if (nplanes > 1)
  83.         color(CYAN);
  84.  
  85.     /* Bottom */
  86.     pmv(carray[0][0], carray[0][1], carray[0][2]);
  87.     pdr(carray[4][0], carray[4][1], carray[4][2]);
  88.     pdr(carray[5][0], carray[5][1], carray[5][2]);
  89.     pdr(carray[1][0], carray[1][1], carray[1][2]);
  90.     pclos();
  91. }
  92.  
  93.  
  94. main(argc, argv)
  95. {
  96.     double    t, dt = 0.2;
  97.     int    r, dr = 100;
  98.     short    val;
  99.  
  100.     prefsize(300L, 300L);
  101.  
  102.     winopen("cube");
  103.  
  104.     qdevice(KEYBD);
  105.     unqdevice(INPUTCHANGE);
  106.  
  107.     nplanes = getplanes();
  108.  
  109.     color(BLACK);
  110.     clear();
  111.  
  112.     window(-1.5, 1.5, -1.5, 1.5, 9.0, -5.0);
  113.     lookat(0.0, 0.0, 12.0, 0.0, 0.0, 0.0, 0.0);
  114.  
  115.     backface(1);
  116.  
  117.     if (argc == 1)
  118.         polymode(PYM_LINE);
  119.     
  120.     doublebuffer();
  121.     gconfig();
  122.  
  123.     t = 0.0;
  124.  
  125.     do {
  126.         for (r = 0; r < 3600; r += dr) {
  127.             color(BLACK);
  128.             clear();
  129.  
  130.             pushmatrix();
  131.                 scale(0.2, 0.2, 0.2);
  132.                 translate(t, t, t);
  133.                 rotate(r, 'y');
  134.                 rotate(r, 'z');
  135.                 rotate(r, 'x');
  136.                 color(WHITE);
  137.                 drawcube();
  138.                 if (nplanes == 1 && argc > 1) {
  139.                     polymode(PYM_LINE);
  140.                     color(0);
  141.                     drawcube();
  142.                 }
  143.                 if (argc > 1)
  144.                     polymode(PYM_FILL);
  145.  
  146.             popmatrix();
  147.  
  148.             pushmatrix();
  149.                 scale(0.2, 0.2, 0.2);
  150.                 translate(t, t, t);
  151.                 rotate(r, 'y');
  152.                 rotate(r, 'z');
  153.                 rotate(r, 'x');
  154.                 color(WHITE);
  155.                 drawcube();
  156.                 if (nplanes == 1 && argc > 1) {
  157.                     polymode(PYM_LINE);
  158.                     color(0);
  159.                     drawcube();
  160.                 }
  161.                 if (argc > 1)
  162.                     polymode(PYM_FILL);
  163.  
  164.             popmatrix();
  165.  
  166.             pushmatrix();
  167.                 scale(0.2, 0.2, 0.2);
  168.                 translate(t, t, t);
  169.                 rotate(r, 'y');
  170.                 rotate(r, 'z');
  171.                 rotate(r, 'x');
  172.                 color(WHITE);
  173.                 drawcube();
  174.                 if (nplanes == 1 && argc > 1) {
  175.                     polymode(PYM_LINE);
  176.                     color(0);
  177.                     drawcube();
  178.                 }
  179.                 if (argc > 1)
  180.                     polymode(PYM_FILL);
  181.  
  182.             popmatrix();
  183.  
  184.             pushmatrix();
  185.                 scale(0.2, 0.2, 0.2);
  186.                 translate(t, t, t);
  187.                 rotate(r, 'y');
  188.                 rotate(r, 'z');
  189.                 rotate(r, 'x');
  190.                 color(WHITE);
  191.                 drawcube();
  192.                 if (nplanes == 1 && argc > 1) {
  193.                     polymode(PYM_LINE);
  194.                     color(0);
  195.                     drawcube();
  196.                 }
  197.                 if (argc > 1)
  198.                     polymode(PYM_FILL);
  199.  
  200.             popmatrix();
  201.  
  202.             pushmatrix();
  203.                 scale(0.2, 0.2, 0.2);
  204.                 translate(t, t, t);
  205.                 rotate(r, 'y');
  206.                 rotate(r, 'z');
  207.                 rotate(r, 'x');
  208.                 color(WHITE);
  209.                 drawcube();
  210.                 if (nplanes == 1 && argc > 1) {
  211.                     polymode(PYM_LINE);
  212.                     color(0);
  213.                     drawcube();
  214.                 }
  215.                 if (argc > 1)
  216.                     polymode(PYM_FILL);
  217.  
  218.             popmatrix();
  219.  
  220.             pushmatrix();
  221.                 scale(0.2, 0.2, 0.2);
  222.                 translate(t, t, t);
  223.                 rotate(r, 'y');
  224.                 rotate(r, 'z');
  225.                 rotate(r, 'x');
  226.                 color(WHITE);
  227.                 drawcube();
  228.                 if (nplanes == 1 && argc > 1) {
  229.                     polymode(PYM_LINE);
  230.                     color(0);
  231.                     drawcube();
  232.                 }
  233.                 if (argc > 1)
  234.                     polymode(PYM_FILL);
  235.  
  236.             popmatrix();
  237.  
  238.             pushmatrix();
  239.                 scale(0.2, 0.2, 0.2);
  240.                 translate(t, t, t);
  241.                 rotate(r, 'y');
  242.                 rotate(r, 'z');
  243.                 rotate(r, 'x');
  244.                 color(WHITE);
  245.                 drawcube();
  246.                 if (nplanes == 1 && argc > 1) {
  247.                     polymode(PYM_LINE);
  248.                     color(0);
  249.                     drawcube();
  250.                 }
  251.                 if (argc > 1)
  252.                     polymode(PYM_FILL);
  253.  
  254.             popmatrix();
  255.  
  256.             pushmatrix();
  257.                 scale(0.2, 0.2, 0.2);
  258.                 translate(t, t, t);
  259.                 rotate(r, 'y');
  260.                 rotate(r, 'z');
  261.                 rotate(r, 'x');
  262.                 color(WHITE);
  263.                 drawcube();
  264.                 if (nplanes == 1 && argc > 1) {
  265.                     polymode(PYM_LINE);
  266.                     color(0);
  267.                     drawcube();
  268.                 }
  269.                 if (argc > 1)
  270.                     polymode(PYM_FILL);
  271.  
  272.             popmatrix();
  273.  
  274.             pushmatrix();
  275.                 scale(0.2, 0.2, 0.2);
  276.                 translate(t, t, t);
  277.                 rotate(r, 'y');
  278.                 rotate(r, 'z');
  279.                 rotate(r, 'x');
  280.                 color(WHITE);
  281.                 drawcube();
  282.                 if (nplanes == 1 && argc > 1) {
  283.                     polymode(PYM_LINE);
  284.                     color(0);
  285.                     drawcube();
  286.                 }
  287.                 if (argc > 1)
  288.                     polymode(PYM_FILL);
  289.  
  290.             popmatrix();
  291.  
  292.             pushmatrix();
  293.                 scale(0.2, 0.2, 0.2);
  294.                 translate(t, t, t);
  295.                 rotate(r, 'y');
  296.                 rotate(r, 'z');
  297.                 rotate(r, 'x');
  298.                 color(WHITE);
  299.                 drawcube();
  300.                 if (nplanes == 1 && argc > 1) {
  301.                     polymode(PYM_LINE);
  302.                     color(0);
  303.                     drawcube();
  304.                 }
  305.                 if (argc > 1)
  306.                     polymode(PYM_FILL);
  307.  
  308.             popmatrix();
  309.  
  310.             pushmatrix();
  311.                 scale(0.2, 0.2, 0.2);
  312.                 translate(t, t, t);
  313.                 rotate(r, 'y');
  314.                 rotate(r, 'z');
  315.                 rotate(r, 'x');
  316.                 color(WHITE);
  317.                 drawcube();
  318.                 if (nplanes == 1 && argc > 1) {
  319.                     polymode(PYM_LINE);
  320.                     color(0);
  321.                     drawcube();
  322.                 }
  323.                 if (argc > 1)
  324.                     polymode(PYM_FILL);
  325.  
  326.             popmatrix();
  327.  
  328.             pushmatrix();
  329.                 scale(0.1, 0.1, 0.1);
  330.                 translate(0.0, t*2, t);
  331.                 rotate(r, 'y');
  332.                 rotate(r, 'z');
  333.                 rotate(r, 'x');
  334.                 color(WHITE);
  335.                 drawcube();
  336.                 if (nplanes == 1 && argc > 1) {
  337.                     polymode(PYM_LINE);
  338.                     color(0);
  339.                     drawcube();
  340.                 }
  341.                 if (argc > 1)
  342.                     polymode(PYM_FILL);
  343.  
  344.             popmatrix();
  345.  
  346.             pushmatrix();
  347.                 scale(0.5, 0.5, 0.5);
  348.                 translate(0.0, t, t);
  349.                 rotate(r, 'y');
  350.                 rotate(r, 'z');
  351.                 rotate(r, 'x');
  352.                 color(WHITE);
  353.                 drawcube();
  354.                 if (nplanes == 1 && argc > 1) {
  355.                     polymode(PYM_LINE);
  356.                     color(0);
  357.                     drawcube();
  358.                 }
  359.                 if (argc > 1)
  360.                     polymode(PYM_FILL);
  361.  
  362.             popmatrix();
  363.  
  364.             pushmatrix();
  365.                 scale(0.5, 0.5, 0.5);
  366.                 translate(0.0, t, t);
  367.                 rotate(r, 'y');
  368.                 rotate(r, 'z');
  369.                 rotate(r, 'x');
  370.                 color(WHITE);
  371.                 drawcube();
  372.                 if (nplanes == 1 && argc > 1) {
  373.                     polymode(PYM_LINE);
  374.                     color(0);
  375.                     drawcube();
  376.                 }
  377.                 if (argc > 1)
  378.                     polymode(PYM_FILL);
  379.  
  380.             popmatrix();
  381.  
  382.             pushmatrix();
  383.                 scale(0.5, 0.5, 0.5);
  384.                 translate(t, 0.0, t);
  385.                 rotate(r, 'y');
  386.                 rotate(r, 'z');
  387.                 rotate(r, 'x');
  388.                 color(WHITE);
  389.                 drawcube();
  390.                 if (nplanes == 1 && argc > 1) {
  391.                     polymode(PYM_LINE);
  392.                     color(0);
  393.                     drawcube();
  394.                 }
  395.                 if (argc > 1)
  396.                     polymode(PYM_FILL);
  397.  
  398.             popmatrix();
  399.  
  400.             pushmatrix();
  401.                 scale(0.5, 0.5, 0.5);
  402.                 translate(0.0, 0.0, t);
  403.                 rotate(r, 'y');
  404.                 rotate(r, 'z');
  405.                 rotate(r, 'x');
  406.                 color(WHITE);
  407.                 drawcube();
  408.                 if (nplanes == 1 && argc > 1) {
  409.                     polymode(PYM_LINE);
  410.                     color(0);
  411.                     drawcube();
  412.                 }
  413.                 if (argc > 1)
  414.                     polymode(PYM_FILL);
  415.  
  416.             popmatrix();
  417.  
  418.             pushmatrix();
  419.                 scale(0.5, 0.5, 0.5);
  420.                 translate(t, 0.0, 0.0);
  421.                 rotate(r, 'y');
  422.                 rotate(r, 'z');
  423.                 rotate(r, 'x');
  424.                 color(WHITE);
  425.                 drawcube();
  426.                 if (nplanes == 1 && argc > 1) {
  427.                     polymode(PYM_LINE);
  428.                     color(0);
  429.                     drawcube();
  430.                 }
  431.                 if (argc > 1)
  432.                     polymode(PYM_FILL);
  433.  
  434.             popmatrix();
  435.  
  436.             pushmatrix();
  437.                 scale(0.5, 0.5, 0.5);
  438.                 translate(0.0, t, 0.0);
  439.                 rotate(r, 'y');
  440.                 rotate(r, 'z');
  441.                 rotate(r, 'x');
  442.                 color(WHITE);
  443.                 drawcube();
  444.                 if (nplanes == 1 && argc > 1) {
  445.                     polymode(PYM_LINE);
  446.                     color(0);
  447.                     drawcube();
  448.                 }
  449.                 if (argc > 1)
  450.                     polymode(PYM_FILL);
  451.  
  452.             popmatrix();
  453.  
  454.             pushmatrix();
  455.                 scale(0.5, 0.5, 0.5);
  456.                 translate(0.0, 0.0, t);
  457.                 rotate(r, 'y');
  458.                 rotate(r, 'z');
  459.                 rotate(r, 'x');
  460.                 color(WHITE);
  461.                 drawcube();
  462.                 if (nplanes == 1 && argc > 1) {
  463.                     polymode(PYM_LINE);
  464.                     color(0);
  465.                     drawcube();
  466.                 }
  467.                 if (argc > 1)
  468.                     polymode(PYM_FILL);
  469.  
  470.             popmatrix();
  471.  
  472.             t += dt;
  473.             if (t > 3.0 || t < -18.0)
  474.                 dt = -dt;
  475.  
  476.             swapbuffers();
  477.  
  478.             if (qtest()) {
  479.                 qread(&val);
  480.                 gexit();
  481.                 exit(0);
  482.             }
  483.         }
  484.     } while(1);
  485. }
  486.  
  487.